import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, ListData, DetailData} from './DTO';
const baseUrl = '/parts/api/v1';

const getInitDesc = formatMessage({
    id: 'api.getInit',
    defaultMessage: '数据初始化'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询报关清关列表'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询报关清关列表详情'
});

const getOnDeclareSubmitDesc = formatMessage({
    id: 'api.getOnDeclareSubmit',
    defaultMessage: '提交报关信息'
});

const getOnClearanceSubmitDesc = formatMessage({
    id: 'api.getOnClearanceSubmitDesc',
    defaultMessage: '提交清关信息'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/customsClearance/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitDesc));

// 查询报关清关列表
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/customsClearances/forCurrentUser?${queryString}`, null, ListData)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询报关清关列表详情
export const getDetail = id => request(`${baseUrl}/customsClearances/${id}`, null, DetailData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 报关提交
export const onDeclareSubmit = (id, data) => request(`${baseUrl}/customsClearances/${id}/declare`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getOnDeclareSubmitDesc));

export const onClearanceSubmit = (id, data) => request(`${baseUrl}/customsClearances/${id}/clearance`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getOnClearanceSubmitDesc));

